package com.siki;

import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@MapperScan("com.siki.dao")
@EnableSwagger2
@SpringBootApplication
public class RegistrationsystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(RegistrationsystemApplication.class, args);
    }

    //下面Bean作用是将http请求跳转到https
    //如果以后需要维护，这里是不需要动的
    //
//    @Bean
    public Connector connector(){
        Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol");
        connector.setScheme("http");
        connector.setPort(8080);
        connector.setSecure(false);
        connector.setRedirectPort(8443);
        return connector;
    }
//    @Bean
    public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);

            }
        };
        tomcat.addConnectorCustomizers(connectors -> {
            if (connectors.getProtocolHandler() instanceof AbstractHttp11Protocol){
                AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) connectors
                        .getProtocolHandler();
                protocol.setDisableUploadTimeout(false);
                protocol.setConnectionUploadTimeout(5000);
            }
        });
        tomcat.addAdditionalTomcatConnectors(connector);
        return tomcat;
    }

}
